package com.burgeon.backend.SHOPBACKEND.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.Employee;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class QuerryEmpDataByInId extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public QuerryEmpDataByInId(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Employee> getData(String[] ids) {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = null;
        try {
            str = RESULT(Data(ids), url);


        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Employee> employees = new ArrayList<Employee>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);
            Employee employee = new Employee();
            employee.setId(array.getString(0));
            employee.setNo(array.getString(1));
            employee.setIsSaler(array.getString(2));
            employee.setName(array.getString(3));
            employee.setBirthday(array.getString(4));
            employee.setPhone(array.getString(5));
            employee.setState(array.getString(6));
            employee.setBeginDate(array.getString(7));
            employee.setSex(array.getString(8));
            employee.setStore(array.getString(9));
            employees.add(employee);
        }
            return employees;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<Employee>();
        }

    }

    private MultiValueMap Data(String[] ids) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("NO");
        columns.add("ISSALER");//是否营业员
        columns.add("NAME");
        columns.add("BIRTHDATE");
        columns.add("PHONE");
        columns.add("INCUMBENCY_STS");//在职状况
        columns.add("BEGIN_DATE");//入职日期
        columns.add("SEX");
        columns.add("'C_STORE_ID;NAME'");

        params.put("columns", columns);
        params.put("table", "C_V_EMPLOYEE");//表名

        params.put("count", "true");

        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号

        paramss.put("column", "'ID'");
        paramss.put("condition", toString(ids));
        params.put("range",1000);
        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }
    private String toString(String[] arr) {

        StringBuilder stringBuffer = new StringBuilder();
        for (String str : arr) {
            stringBuffer.append("'")
                    .append(str)
                    .append("'")
                    .append(",");;
        }

        return "\" IN ("+
                stringBuffer.replace(stringBuffer.length() - 1, stringBuffer.length(), "")
                +")\""
                ;

    }
    public static void main(String[] args) {
        QuerryEmpDataByInId querryYCDisStatus = new QuerryEmpDataByInId("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        String[] ids = new String[]{"5138","5141","6392","5144","5145","5148","5153","5154","5156","5158","5203","5205","5206","5215","5216","5220","5221","5223","5227","5229","5230","5253","5259","5278","5168","5171","5172","5173","6236","5176","5177","5179","6238","5184","5185","5194","5196","5104","5135","6246","5982","6248","6249","6252","6253","6263","6267","6268","6327","5912","5922","5937","6270","6403","5830","5907","6064","6474","6482","5872","6047","6254","6468","6007","6037","6100","6335","6460","5870","5862","5948","5958","5976","6078","6676","6702","6791","6894","6642","6646","6772","6807","6538","6545","6596","6614","6812","6653","6795","6802","6803","6663","6714","6843","6847","6874","6882","6568","5186","6273","6737","6778","6943","6994","7038","7093","7094","7113","7146","7179","5228","6959","6995","7043","7097","7123","7133","7163","7178","6920","6960","7180","6929","6954","6973","6988","7018","7059","7148","7171","7175","7177","6953","7014","7058","7079","7091","7098","7105","7120","7138","7176","7108","7155","7173","7188","7095","7096","7128","7174","6934","7017","7063","7170","7183"};
        try {
            System.err.println(querryYCDisStatus.getData(ids));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
